home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
cstream.exe
/
CSDEMO.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1991-06-07
|
5KB
|
244 lines
/*
csdemo.cpp
6-7-91
class stream demo
Copyright 1991
John W. Small
All rights reserved
Use freely but acknowledge authorship and copyright.
CIS: 73757,2233
PSW / Power SoftWare
P.O. Box 10072
McLean, Virginia 22102 8072
USA (703) 759-3838
*/
#include <string.h>
#include <fstream.h>
#include <cstream.hpp>
#define MAX_STR_BUF 80
class Employee : public StreamableClass {
char *name, *address, *cityStateZip;
static char nameBuf[], addressBuf[], cityStateZipBuf[];
void construct(char *name, char *address,
char *cityStateZip)
{
this->name = strdup(name);
this->address = strdup(address);
this->cityStateZip = strdup(cityStateZip);
}
public:
StreamableClassID(Employee,StreamableClass,2);
Employee(char *name, char *address,
char *cityStateZip)
: StreamableClass(CLASS_ID)
{
construct(name,address,cityStateZip);
}
~Employee() { delete name; delete address;
delete cityStateZip; }
};
typedef Employee * EmployeE;
#define EmployeE0 ((EmployeE)0)
char Employee::nameBuf[MAX_STR_BUF];
char Employee::addressBuf[MAX_STR_BUF];
char Employee::cityStateZipBuf[MAX_STR_BUF];
void Employee::store(ostream& os)
{
os << name << endf << address << endf
<< cityStateZip << endf;
}
StreamC Employee::load(istream& is, StreamC C)
{
if (!C) if ((C = (StreamC) new Employee())
== StreamC0)
return C;
is.getline(nameBuf,MAX_STR_BUF,
StreamableClass::FieldTermChar);
is.getline(addressBuf,MAX_STR_BUF,
StreamableClass::FieldTermChar);
is.getline(cityStateZipBuf,MAX_STR_BUF
,StreamableClass::FieldTermChar);
((EmployeE)C)->construct(nameBuf,addressBuf,
cityStateZipBuf);
return C;
}
class Product : StreamableClass {
char *name, * type;
unsigned price;
static char nameBuf[], typeBuf[];
static unsigned priceBuf;
void construct(char *name, char *type,
unsigned price)
{
this->name = strdup(name);
this->type = strdup(type);
this->price = price;
}
public:
StreamableClassID(Product,StreamableClass,3);
Product(char *name, char *type, unsigned price)
: StreamableClass(CLASS_ID)
{ construct(name,type,price); }
~Product() { delete name; delete type; }
};
typedef Product * ProducT;
#define ProducT0 ((ProducT)0)
char Product::nameBuf[MAX_STR_BUF];
char Product::typeBuf[MAX_STR_BUF];
unsigned Product::priceBuf;
void Product::store(ostream& os)
{
os << name << endf << type << endf
<< price << endf;
}
StreamC Product::load(istream& is, StreamC C)
{
if (!C) if ((C = (StreamC) new Product())
== StreamC0)
return C;
is.getline(nameBuf,MAX_STR_BUF
,StreamableClass::FieldTermChar);
is.getline(typeBuf,MAX_STR_BUF
,StreamableClass::FieldTermChar);
is >> priceBuf;
is.get(); // field term char
((ProducT)C)->construct(nameBuf,typeBuf,priceBuf);
return C;
}
class Special : Product {
unsigned specialPrice;
static unsigned spriceBuf;
void construct(unsigned specialPrice)
{ this->specialPrice = specialPrice; }
public:
StreamableClassID(Special,Product,4);
Special(char *name, char *type, unsigned price,
unsigned specialPrice)
: Product(name,type,price)
{
setID(CLASS_ID);
construct(specialPrice);
}
~Special() {return;}
};
typedef Special * SpeciaL;
#define SpeciaL0 ((SpeciaL)0)
unsigned Special::spriceBuf;
void Special::store(ostream& os)
{
Product::store(os);
os << specialPrice << endf;
}
StreamC Special::load(istream& is, StreamC C)
{
if (!C) if ((C = (StreamC) new Special())
== StreamC0)
return C;
Product::load(is,C);
is >> spriceBuf;
is.get(); // field term char
((SpeciaL)C)->construct(spriceBuf);
return C;
}
StreamableClasses(20);
main()
{
RegisterClass(Employee);
RegisterClass(Product);
RegisterClass(Special);
Employee e1("Frank Borland","Sierra Nevada Mts.",
"California");
Employee e2("Philippe Kahn","1800 Green Hills Road",
"Scotts Valley, CA 95067");
Employee e3("Mike Slater","1700 Green Hills Road",
"Scotts Valley, CA 95067");
Product p1("Borland C++","Language",495);
Product p2("Quatro Pro 3.0","Business/GUI",99);
Product p3("Paradox Engine 2.0","Applications",99);
Special sp1("Borland C++","Language",495,99);
cout << "Stream three Employee Classes\n" << endl;
cout << e1 << e2 << e3 << "press enter ... " << flush;
cin.get(); cout << endl;
cout << "Stream three Product Classes\n" << endl;
cout << p1 << p2 << p3 << "press enter ... " << flush;
cin.get(); cout << endl;
cout << "Stream one Special Product Class\n" << endl;
cout << sp1 << "press enter ... " << flush;
cin.get(); cout << endl;
ofstream oS("emp$prod.cls");
if (!oS)
return 1;
oS << p1 << e2 << p3 << e1 << p2 << e3 << sp1;
oS.close();
ifstream iS("emp$prod.cls");
if (!iS)
return 2;
StreamableClass *S1, *S2, *S3;
iS >> S1 >> S2 >> S3;
cout << "Stream three classes\n" << endl;
cout << S1 << S2 << S3 << "press enter ... " << flush;
cin.get(); cout << endl;
delete S1; delete S2; delete S3;
iS >> S1 >> S2 >> S3;
cout << "Stream three more classes\n" << endl;
cout << S1 << S2 << S3 << "press enter ... " << flush;
cin.get(); cout << endl;
delete S1; delete S2; delete S3;
iS >> S1;
cout << "Stream one last class\n" << endl;
cout << S1 << "press enter ... " << flush;
cin.get(); cout << endl;
delete S1;
return 0;
}